package com.fingard.dsp.bank.directbank.bocom15;

import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.WebRequest;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCOM15AcceptNotice extends BOCOM15Base implements IAcceptNotice {
    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
        NoticeResp resp = new NoticeResp();
        try{
            String p_req = pNtReq.getReqString();
            WriteBankLogLn("接收到建信交行通知报文：" + pNtReq.getFullString());
            if(p_req.length()==0){
                String tmpStrRet = "空报文";
                WriteBankLogLn("响应："+tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            }
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(pNtReq.getFullString(), tmpSbMsg);
            String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
            XmlTextReader tmpBkReaders = loadBankRespXmlReader(returnMsg, tmpSbMsg);
            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.writeStartDocument();
            xmlWriter.writeStartElement("ATSYH");
            xmlWriter.writeStartElement("TransResp");
            xmlWriter.writeElementString("BankCode", "BOCOM15");
            xmlWriter.writeElementString("TransType", "ContractNotify");
            xmlWriter.writeElementString("TransSeqID", tmpBkReaders.getFirstText("/Req/Head/Sender/SeqNo"));
            xmlWriter.writeStartElement("TransParam");
            xmlWriter.writeStartElement("DetailRecord");
            xmlWriter.writeElementString("RespCode", "0");
            xmlWriter.writeElementString("RespInfo", "成功");
            xmlWriter.writeElementString("ReqSeqID", tmpBkReaders.getFirstText("/Req/Body/SeqNo"));
            xmlWriter.writeElementString("PayAct", tmpBkReaders.getFirstText("/Req/Body/ActNo"));

            xmlWriter.writeElementString("SignDate", tmpBkReaders.getFirstText("/Req/Body/BusDate"));
            xmlWriter.writeElementString("SourceNoteCode", tmpBkReaders.getFirstText("/Req/Body/ApplyNo"));
            xmlWriter.writeElementString("PolicyNo", tmpBkReaders.getFirstText("/Req/Body/PolNo"));
            xmlWriter.writeElementString("OppActName", tmpBkReaders.getFirstText("/Req/Body/Name"));
            xmlWriter.writeElementString("CertType", convertCertTypeToBank(tmpBkReaders.getFirstText("/Req/Body/IdType")));
            xmlWriter.writeElementString("CertNumber", tmpBkReaders.getFirstText("/Req/Body/IdNo"));
            xmlWriter.writeElementString("CellPhone", tmpBkReaders.getFirstText("/Req/Body/Telephone"));
            xmlWriter.writeElementString("CurType", tmpBkReaders.getFirstText("/Req/Body/CurType"));
            xmlWriter.writeElementString("NoteMoney", tmpBkReaders.getFirstText("/Req/Body/Amt"));
            xmlWriter.writeElementString("BusType", tmpBkReaders.getFirstText("/Req/Body/BusType"));
            if("1".equals(tmpBkReaders.getFirstText("/Req/Body/AgreementStat"))){
                xmlWriter.writeElementString("SignState", "success");
            }else {
                xmlWriter.writeElementString("SignState", "fail");
            }
            xmlWriter.writeElementString("AuthCode", tmpBkReaders.getFirstText("/Req/Body/AgreementNo"));
            xmlWriter.writeEndDocument();
            String url = getBankConfig().getValueAsString("notifyURL");
            WriteBankLogLn("通知630的地址：" + url);
            String p_reqStr = xmlWriter.toXmlString();
            WriteBankLogLn("通知630的报文：" + p_reqStr);
            byte[] tmpToWriteBytes = p_reqStr.getBytes(getCharset());
            WebRequest tmpWebRequest = new WebRequest(url);
            tmpWebRequest.setConnectTimeout(600000);
            tmpWebRequest.setRequestProperty("Content-Type", "application/xml");
            tmpWebRequest.setRequestProperty("Connection", "keep-alive");
            tmpWebRequest.upload(tmpToWriteBytes, getCharset());
        }catch(Exception e){
            WriteBankLogLn(e);
        }
        return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {

    }
}
